import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import envCfg from "./assets/js/env";
import './components'
import Element from "element-ui"
import 'element-ui/lib/theme-chalk/index.css';
import "./assets/css/index.css"

import i18n from './i18n/langs/index.js'
Vue.use(Element, i18n)

import moment from "moment"
import axios from "axios"

Vue.prototype.$axios = axios
Vue.prototype.$moment = moment

Vue.config.productionTip = false;

Vue.filter('dateFormat', function (date) {
    return moment(date).format('YYYY-MM-DD')
})
Vue.filter('dateFormats', function (date) {
    if (date == '' || date == null) {
        return ''
    } else {
        return moment(date).format('YYYY-MM-DD')
    }
})
// joya.js 已经在Ynavi.js动态引入
// var jaq = jaq || [];
// jaq.push(['account', 'JA2020_1532795']); //站点编号
// jaq.push(['domain', 'ciftis.org']); //站点域名
// jaq.push(['anchorpvflag', 'true']);
// (function () {
//     var ja = document.createElement('script'); ja.type = 'text/javascript';
//     ja.async = true;
//     ja.src = '//wl.jd.com/joya.js';
//     var s = document.getElementsByTagName('script')[0];
//     s.parentNode.insertBefore(ja, s);
// })();
// 加载第三方项目的js
const sNode = document.createElement('script')
// const sJaq = document.createElement('script')
sNode.type = 'text/javascript'

sNode.src = 'https://navi-back.ciftis.org/Ynavi.js'
// sNode.src = 'https://navi-back-test.ciftis.org.cn/Ynavi.js'

document.body.appendChild(sNode)
axios.defaults.timeout = 50000;//超时时间
// 请求拦截器 - header中添加channelCode属性
axios.interceptors.request.use(
    config => {
        config.headers.channelCode = 'cm-pc'
		
        if (config.method === 'get') {
            config.params && (config.params.timestamp = new Date().getTime())
        }

        return config
    },
    error => {
        return Promise.error(error)
    }
);

// 全局拦截401 未登录
axios.interceptors.response.use(
    response => {
        if (response.data.code === 401) {
            // 重定向到 login
            window.location.href = envCfg.loginUrl + encodeURIComponent(envCfg.selfUrl)
        }
        if (response.data.code === 403) {
            // 跳转无权限
            window.location.href = envCfg.authority
        }
		if (response.data.code === 429) {
		    // 554
		    window.location.href = 'https://error.ciftis.org/busy.html?returnUrl=' + encodeURIComponent(window.location.href)
		}
        return response.data
    },
    error => {
        return error
    })

getDictionarys()
async function getDictionarys () {
    // 字典接口
    const res = await axios.post('/api/dictionary/listDictionarysGroupBy', {})
    if (res.code == 200) {
        // 表格数据
        store.commit('listDictionarys', res.data)
    } else {
        console.log(res)
    }
}

// // 首页导出按钮显示
axios.post("/api/dictionary/getUserInfo",{})
    .then(res => {
        console.log(res)
        if (res.code === 200) {
            store.commit('userInfo', res.data)
        }
    })
new Vue({
    router,
    store,
    i18n,
    render: h => h(App)
}).$mount("#app");
